package com.ecable.action;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ecable.exception.ConnectionException;
import com.ecable.exception.DataNotFoundException;
import com.ecable.bean.VillageTO;
import com.ecable.delegate.VillageMgrDelegate;
import com.ecable.util.UtilConstants;

public class VillageUpdateAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = "";
		VillageTO vvto = null;
		try {
			try {
				int vid = Integer.parseInt(request.getParameter("vid"));
				vvto = new VillageMgrDelegate().viewVillageAt(vid);
			} catch (ConnectionException e) {
				throw new ServletException("ConnectionTO Failed");
			} catch (DataNotFoundException e) {
				throw new ServletException("No Data found");
			}
			if (vvto != null) {
				request.setAttribute("status", UtilConstants._ALL_VILLAGES);
				request.setAttribute("villageTo", vvto);
				path = UtilConstants._UPDATE_VILLAGE_HOME;
			} else {
				request.setAttribute("status",
						UtilConstants._ALL_VILLAGE_FAILED);
				path = UtilConstants._UPDATE_VILLAGE_HOME;
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("status", UtilConstants._INVALIED_ENTRY);
			path = UtilConstants._UPDATE_VILLAGE_HOME;
		}
		RequestDispatcher rd = request.getRequestDispatcher(path);
		rd.forward(request, response);
	}
}
